package com.sinochem.yunlian.ship.base.rpc;

import com.sinochem.yunlian.ship.api.vo.bs.BsInsuranceProductVo;
import com.sinochem.yunlian.ship.api.vo.bs.CityVo;
import com.sinochem.yunlian.ship.base.biz.BsCityBiz;
import com.sinochem.yunlian.ship.base.biz.BsDataDictItemBiz;
import com.sinochem.yunlian.ship.base.biz.BsInsuranceProductBiz;
import com.sinochem.yunlian.ship.base.entity.BsCity;
import com.sinochem.yunlian.ship.base.entity.BsInsuranceProduct;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.constant.DeleteStatus;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import com.sinochem.yunlian.ship.common.enums.EnableStatus;
import com.sinochem.yunlian.ship.common.enums.InsuranceType;
import com.sinochem.yunlian.ship.common.enums.InsurantCompany;
import com.sinochem.yunlian.ship.common.enums.ResponseCode;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jayway on 2017/12/19.
 */
@IgnoreToken
@RestController
@RequestMapping("/api/insuranceProduct")
public class BsInsuranceProductRest {

    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private BsInsuranceProductBiz bsInsuranceProductBiz;

    @Autowired
    private BsDataDictItemBiz bsDataDictItemBiz;



    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public RestResponse<BsInsuranceProductVo> findById(@PathVariable("id") Long id) {
        RestResponse restResponse = new RestResponse();
        BsInsuranceProductVo vo = null  ;
        BsInsuranceProduct bsInsuranceProduct = bsInsuranceProductBiz.selectById(id);
        if (bsInsuranceProduct == null) {
            restResponse.setCode(ResponseCode.NOTIFICATION.getCode());
            restResponse.setMsg("无效的产品Id");
            return restResponse;
        }
        vo = new BsInsuranceProductVo();
        BeanUtils.copyProperties(bsInsuranceProduct,vo);
        vo.setCompanyName(InsurantCompany.valueOf(bsInsuranceProduct.getCompanyCode()));
        vo.setInsuranceTypeName(InsuranceType.valueOf(bsInsuranceProduct.getInsuranceType()));
        restResponse.setData(vo);
        return restResponse;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public RestResponse<Boolean> modify(@RequestBody BsInsuranceProductVo productVo) {
        RestResponse restResponse = new RestResponse();
        restResponse.setData(false);
        BsInsuranceProduct product = bsInsuranceProductBiz.selectById(productVo.getId());
        if (product == null) {
            restResponse.setCode(ResponseCode.NOTIFICATION.getCode());
            restResponse.setMsg("无效的产品Id");
            return restResponse;
        }
        product.setDescription(productVo.getDescription());
        product.setSurveyAgent(productVo.getSurveyAgent());
        if (productVo.getEnableStatus() != null) {
            product.setEnableStatus(productVo.getEnableStatus());
        }
        product.setInsuranceRate(productVo.getInsuranceRate());
        product.setInsuranceType(productVo.getInsuranceType());
        product.setUpdateBy(productVo.getUpdateBy());
        product.setUpdateTime(productVo.getUpdateTime());
        bsInsuranceProductBiz.updateSelectiveById(product);
        restResponse.setData(true);
        return restResponse;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST,consumes = "application/json")
    public RestResponse<Long> add(@RequestBody BsInsuranceProductVo productVo) {
        RestResponse restResponse = new RestResponse();
        BsInsuranceProduct product = new BsInsuranceProduct();
        product.setCompanyCode(productVo.getCompanyCode());
        product.setInsuranceType(productVo.getInsuranceType());
        product.setInsuranceCode(productVo.getInsuranceCode());
        product.setDescription(productVo.getDescription());
        product.setSurveyAgent(productVo.getSurveyAgent());
        product.setEnableStatus(productVo.getEnableStatus());
        product.setInsuranceRate(productVo.getInsuranceRate());
        product.setInsuranceType(productVo.getInsuranceType());
        product.setDeleteStatus( DeleteStatus.STATUS_ENABLE);
        product.setEnableStatus(EnableStatus.ENABLE.getCode());
        product.setCreateBy(productVo.getCreateBy());
        product.setCreateTime(productVo.getCreateTime());
        product.setUpdateBy(productVo.getUpdateBy());
        product.setUpdateTime(productVo.getUpdateTime());
        bsInsuranceProductBiz.insert(product);
        restResponse.setData(product.getId());
        return restResponse;
    }


    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public RestResponse list() {
        List<BsInsuranceProduct> list = bsInsuranceProductBiz.findAll();
        List<BsInsuranceProductVo> voList = new ArrayList<>();
        for (BsInsuranceProduct product : list) {
            BsInsuranceProductVo vo = new BsInsuranceProductVo();
            BeanUtils.copyProperties(product,vo);
            vo.setCompanyName(InsurantCompany.valueOf(product.getCompanyCode()));
            vo.setInsuranceTypeName(InsuranceType.valueOf(product.getInsuranceType()));
            voList.add(vo);
        }
        return RestResponse.success(voList);
    }

    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    public RestResponse findByCondition(@RequestBody BsInsuranceProductVo productVo) {

        Map<String,Object> params = new HashMap<>();
        params.put("companyCode",productVo.getCompanyCode());
        params.put("enableStatus",productVo.getEnableStatus());
        params.put("insuranceType",productVo.getInsuranceType());
        List<BsInsuranceProduct> list = bsInsuranceProductBiz.findByConditon(params);
        List<BsInsuranceProductVo> voList = new ArrayList<>();
        for (BsInsuranceProduct product : list) {
            BsInsuranceProductVo vo = new BsInsuranceProductVo();
            BeanUtils.copyProperties(product,vo);
            voList.add(vo);
        }
        return RestResponse.success(voList);
    }


}
